package com.cneport.transfer.user.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户登录
 * @author wudi 2012-7-18
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /** 读取配置文件 */
    public static final ResourceBundle RESOURCE = ResourceBundle
            .getBundle("filePath");

    /**
     * 日志信息
     */
    private static final Log LOG = LogFactory.getLog(UserController.class);

    /**
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseParam = new HashMap();

        String clientId = request.getParameter("userName");
        String inputPassWord = request.getParameter("password");

        try {
            String clientName = RESOURCE.getString("clientName").trim();
            String passWord = RESOURCE.getString("passWord").trim();

            if (clientName.equalsIgnoreCase(clientId)
                    && passWord.equalsIgnoreCase(inputPassWord)) {
                responseParam.put("success", true);
                responseParam.put("info", "账号密码错误");
                request.getSession().setAttribute("userName", clientName);
            } else {
                responseParam.put("success", false);
                responseParam.put("info", "账号密码错误");
            }
        } catch (Exception e) {
            responseParam.put("success", false);
            responseParam.put("info", "系统异常，登录失败");
            LOG.error(e);
        }

        return responseParam;
    }

}
